import { Component, OnInit } from '@angular/core';
import { ToastService, UploadConfig, Item, ConfirmService } from 'ng-tui';
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
import { GlobalService } from '../../../../cores/services';
import { GoodsSpecification, Warehous } from '../../interfaces/warehous.interface';
import { ApiData } from '../../../../cores/classes';
import { skipWhile, switchMap } from 'rxjs/operators';
import { WarehousService } from '../../services/warehous.service';

@Component({
    selector: 'app-basisinfotwo-detail',
    templateUrl: './basisinfotwo-detail.component.html',
    styles: [
        `textarea{min-height:5rem;}`
    ]
})
export class GoodstwoDetailComponent implements OnInit {

    defaultTh = ['单价', '库存', '是否使用'];

    parentTypes = new Array<Item>();

    childTypes = new Array<any>();

    warehous: Warehous = { id: 0, };
    warehouse = new Array<any>();
    options: UploadConfig;

    private stockID: number;
    constructor(
        private router: Router,
        public global: GlobalService,
        private active: ActivatedRoute,
        private toast: ToastService,
        private warehousService: WarehousService,
        private confirm: ConfirmService,
    ) {
        this.options = {
            queryString: '?x-oss-process=image/resize,h_130,w_130',
            uploader: (file: File) => this.warehousService.uploadGoodsImage(file)
        };
        this.active.paramMap.
            pipe(skipWhile(params => !params.has('id')), switchMap<ParamMap, ApiData>(params => {
                this.warehous.id = parseInt(params.get('id'), 10);
                return this.warehousService.getRegion(this.warehous.id);
            }))
            .subscribe(res => {
                this.warehous = res.datas;
            });
    }

    ngOnInit() {
        this.departmentLista();
    }
    /**
       * 仓库列表
       */
    departmentLista() {
        this.warehousService.addhouse()
            .subscribe({
                next: res => {
                    this.warehouse = res.datas;
                },
            });
    }
    /**
     * 确认添加
     */
    confirmInsert(btn: any) {
        this.warehousService.insertRegion(this.warehous).subscribe({
            next: res => {
                this.toast.success('添加成功', `成功添加库区${this.warehous.warehouseRegionName}`);
                // this.goods.id = res.datas.id;
            },
            complete: () => {
                btn.dismiss();
            }
        });
    }

    confirmUpdate(btn: any) {
        this.warehousService.updateRegion(this.warehous).subscribe({
            next: res => {
                this.toast.success('修改成功', `成功修改库区${this.warehous.warehouseRegionName}的信息`);
            },
            complete: () => {
                btn.dismiss();
            }
        });
    }

}
